//
//  WTObject.m
//  study
//
//  Created by codew on 5/31/21.
//

#import "WTObject.h"

@implementation WTObject

static NSThread *thread = nil;
static BOOL runAlways = YES;

+ (NSThread *)threadForDispatch
{
    if (thread == nil) {
        
        @synchronized (self) {
            
            if (thread == nil) {
                
                thread = [[NSThread alloc] initWithTarget:self selector:@selector(request) object:nil];
                [thread setName:@"com.itopengl.thread"];
                [thread start];
            }
        }
    }
    
    return thread;
}


+ (void)request
{
    // 创建一个 Source
    
    CFRunLoopSourceContext context = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,NULL};
    CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
    
    while (runAlways) {
        
        @autoreleasepool {
            
            CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, true);
            
        }
    }
    
    
    CFRunLoopRemoveSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
    CFRelease(source);
    
}
@end
